//
//  WNSettingVC.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/7.
//

import UIKit
import StoreKit

class WNSettingVC: WNBaseVC {
        
    deinit {
        NotificationCenter.default.removeObserver(self, name: WNNotiName.loginNoti, object: nil)
    }

    lazy var setTabView: WNTableView = {
        let tab = WNTableView()
        tab.register(SettingCell.self, forCellReuseIdentifier: SettingCell.identifier)
        tab.backgroundColor = .init(named: "white_F7F7F7")
        tab.delegate = self
        tab.dataSource = self
        tab.rowHeight = 60
        tab.sectionFooterHeight = 10
        tab.isScrollEnabled = false
        return tab
    }()
    
    lazy var logoutBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Login", for: .normal)
        btn.setTitleColor(.init(named: "red_FE95B4"), for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 20)
        btn.backgroundColor = .white
        btn.rx.tap.bind{ [weak self] in
            self?.wn_presentLogin()
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(8)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewConfig() {
        view.addSubview(setTabView)
        view.addSubview(logoutBtn)
        setTabView.snp.makeConstraints { make in
            make.top.equalTo(NaviTotalHeight)
            make.left.bottom.right.equalTo(0)
        }
        logoutBtn.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.bottom.equalTo(-TabBarMargin-scaleHeight(88))
            make.height.equalTo(64)
        }
    }
    
    override func dataConfig() {
        pageName = "Setting"
        navView.titleName = "Setting"
        logoutBtn.isHidden = isAppleLogin()
    }
    
    override func handleNoti() {
        NotificationCenter.default.addObserver(forName: WNNotiName.loginNoti, object: nil, queue: nil) { [weak self] not in
            WNThread.wn_delay(0.1) {
                self?.logoutBtn.isHidden = isAppleLogin()
            }
        }
    }
    
    @objc func refreshNot() {
        WNPhotoManager.shared.wn_notAuthorizeStatus { [weak self] enable in
            saveEnableNot(enable)
            self?.setTabView.reloadRows(at: [IndexPath.init(item: 1, section: 0)], with: .none)
        }
    }
}

extension WNSettingVC: UITableViewDelegate, UITableViewDataSource {
        
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section == 0 ? 3 : 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SettingCell.identifier, for: indexPath) as! SettingCell
        if indexPath.section == 0 {
            cell.openClosure = { isOpen in
                if indexPath.item == 0 {
                    saveAutoOpen(isOpen)
                }else {
                    let url = URL(string: UIApplication.openSettingsURLString)
                    if let url = url, UIApplication.shared.canOpenURL(url) {
                        UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
                        })
                    }
                }
                tableView.reloadRows(at: [indexPath], with: .none)
            }
        }
        cell.settingType = indexPath.section*10+indexPath.item
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = .init(named: "white_F7F7F7")
        return view
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.section {
        case 0:
            switch indexPath.item {
            case 1:
                navigationController?.pushViewController(WNAccountVC(), animated: true)
            case 2:
                navigationController?.pushViewController(WNFeedBackVC(), animated: true)
            default: break
            }
        case 1:
            switch indexPath.item {
            case 0:
                WNShareManager.shared.copyUrlMethod("wqg@clickfun.cn")
                WNHUD.wn_show("Copy email successful")
            case 1:
                navigationController?.pushViewController(WNAboutusVC(), animated: true)
//            case 2:
//                SKStoreReviewController.requestReview()
            case 2:
                if wn_fileSizeOfCache() != 0 {
                    wn_clearCache()
                    WNThread.wn_delay(0.35) {
                        tableView.reloadRows(at: [indexPath], with: .none)
                    }
                }
            default: break
            }
        default:
            break
        }
    }
}
